<template>
  <div class="student-error-collection">
    <!-- 面包屑导航 -->
    <div class="breadcrumb">
      <span class="breadcrumb-item">作业复习</span>
      <span class="breadcrumb-separator">/</span>
      <span class="breadcrumb-item current">我的错题集</span>
    </div>

    <!-- 主内容区域 -->
    <div class="main-content">
      <!-- 页面标题 -->
      <div class="page-header">
        <h1 class="page-title">我的错题集</h1>
        <div class="header-stats">
          <div class="stat-item">
            <span class="stat-value">{{ totalErrors }}</span>
            <span class="stat-label">总错题数</span>
          </div>
          <div class="stat-item">
            <span class="stat-value">{{ reviewedErrors }}</span>
            <span class="stat-label">已复习</span>
          </div>
          <div class="stat-item">
            <span class="stat-value">{{ masteredErrors }}</span>
            <span class="stat-label">已掌握</span>
          </div>
        </div>
      </div>

      <!-- 学科分类标签 -->
      <div class="subject-tabs">
        <div 
          v-for="subject in subjects" 
          :key="subject.key"
          class="subject-tab"
          :class="{ active: activeSubject === subject.key }"
          @click="setActiveSubject(subject.key)"
        >
          {{ subject.label }}
        </div>
      </div>

      <!-- 状态分类标签 -->
      <div class="type-tabs">
        <div 
          v-for="status in statuses" 
          :key="status.key"
          class="type-tab"
          :class="{ active: activeStatus === status.key }"
          @click="setActiveStatus(status.key)"
        >
          {{ status.label }}
        </div>
        
        <!-- 搜索框 -->
        <div class="search-box">
          <input 
            type="text" 
            placeholder="搜索错题内容"
            v-model="searchKeyword"
            class="search-input"
          />
          <el-icon class="search-icon">
            <Search />
          </el-icon>
        </div>
      </div>

      <!-- 错题列表 -->
      <div class="error-list">
        <StudentErrorCard
          v-for="error in filteredErrors"
          :key="error.id"
          :error="error"
          @review="handleReview"
          @practice="handlePractice"
          @master="handleMaster"
        />
      </div>

      <!-- 空状态 -->
      <div v-if="filteredErrors.length === 0" class="empty-state">
        <div class="empty-icon">🎉</div>
        <p class="empty-text">暂无错题记录</p>
        <p class="empty-desc">继续努力学习，争取不出错哦！</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { Search } from '@element-plus/icons-vue'
import StudentErrorCard from '@/components/student/Cards/StudentErrorCard.vue'

const router = useRouter()

// 学科分类数据
const subjects = [
  { key: 'all', label: '全部' },
  { key: 'math', label: '数学' },
  { key: 'english', label: '英语' },
  { key: 'chinese', label: '语文' },
  { key: 'other', label: '其他' }
]

// 状态分类数据 - 学生视角
const statuses = [
  { key: 'all', label: '全部' },
  { key: 'new', label: '新错题' },
  { key: 'reviewing', label: '复习中' },
  { key: 'practiced', label: '已练习' },
  { key: 'mastered', label: '已掌握' }
]

// 当前选中的分类
const activeSubject = ref('all')
const activeStatus = ref('all')
const searchKeyword = ref('')

// 模拟学生错题数据
const errors = ref([
  {
    id: 1,
    title: '分数加减法运算',
    question: '计算：3/4 + 1/6 = ?',
    subject: 'math',
    status: 'new',
    difficulty: '中等',
    wrongAnswer: '5/10',
    correctAnswer: '11/12',
    explanation: '通分后计算：3/4 = 9/12，1/6 = 2/12，所以 9/12 + 2/12 = 11/12',
    taskName: '数学期中测试',
    errorDate: new Date('2024-01-15'),
    reviewCount: 0,
    practiceCount: 0,
    tags: ['分数运算', '通分']
  },
  {
    id: 2,
    title: '英语语法-现在进行时',
    question: 'She _____ (read) a book now.',
    subject: 'english',
    status: 'reviewing',
    difficulty: '简单',
    wrongAnswer: 'reads',
    correctAnswer: 'is reading',
    explanation: '现在进行时的结构是：be动词 + 动词ing形式',
    taskName: '英语语法练习',
    errorDate: new Date('2024-01-14'),
    reviewCount: 2,
    practiceCount: 1,
    tags: ['现在进行时', '语法']
  },
  {
    id: 3,
    title: '古诗词理解',
    question: '"春风又绿江南岸"中的"绿"字有什么妙处？',
    subject: 'chinese',
    status: 'practiced',
    difficulty: '困难',
    wrongAnswer: '表示颜色',
    correctAnswer: '"绿"字用作动词，生动地表现了春风吹过后江南大地一片新绿的景象',
    explanation: '这里"绿"字是动词活用，化静为动，形象生动',
    taskName: '语文阅读理解',
    errorDate: new Date('2024-01-13'),
    reviewCount: 3,
    practiceCount: 2,
    tags: ['古诗词', '修辞手法']
  },
  {
    id: 4,
    title: '几何图形面积',
    question: '一个圆的半径是5cm，求它的面积。',
    subject: 'math',
    status: 'mastered',
    difficulty: '简单',
    wrongAnswer: '25π cm²',
    correctAnswer: '25π cm²',
    explanation: '圆的面积公式：S = πr²，所以 S = π × 5² = 25π cm²',
    taskName: '数学几何练习',
    errorDate: new Date('2024-01-12'),
    reviewCount: 4,
    practiceCount: 3,
    tags: ['圆的面积', '几何']
  }
])

// 统计数据
const totalErrors = computed(() => errors.value.length)
const reviewedErrors = computed(() => errors.value.filter(e => e.reviewCount > 0).length)
const masteredErrors = computed(() => errors.value.filter(e => e.status === 'mastered').length)

// 根据分类和搜索关键词筛选错题
const filteredErrors = computed(() => {
  let filtered = errors.value

  // 按学科筛选
  if (activeSubject.value !== 'all') {
    filtered = filtered.filter(error => error.subject === activeSubject.value)
  }

  // 按状态筛选
  if (activeStatus.value !== 'all') {
    filtered = filtered.filter(error => error.status === activeStatus.value)
  }

  // 按搜索关键词筛选
  if (searchKeyword.value.trim()) {
    filtered = filtered.filter(error => 
      error.title.toLowerCase().includes(searchKeyword.value.toLowerCase()) ||
      error.question.toLowerCase().includes(searchKeyword.value.toLowerCase()) ||
      error.tags.some(tag => tag.toLowerCase().includes(searchKeyword.value.toLowerCase()))
    )
  }

  // 按错误日期倒序排列
  return filtered.sort((a, b) => b.errorDate.getTime() - a.errorDate.getTime())
})

// 设置活跃学科
const setActiveSubject = (subject: string) => {
  activeSubject.value = subject
}

// 设置活跃状态
const setActiveStatus = (status: string) => {
  activeStatus.value = status
}

// 错题操作事件
const handleReview = (error: any) => {
  console.log('复习错题:', error)
  // 增加复习次数
  error.reviewCount++
  if (error.status === 'new') {
    error.status = 'reviewing'
  }
  
  // 跳转到错题详情页面
  router.push({
    name: 'student-error-detail',
    params: { id: error.id },
    query: { context: 'review' }
  })
}

const handlePractice = (error: any) => {
  console.log('练习错题:', error)
  // 增加练习次数
  error.practiceCount++
  if (error.status === 'reviewing') {
    error.status = 'practiced'
  }
  
  // 跳转到练习页面
  router.push({
    name: 'student-error-practice',
    params: { id: error.id },
    query: { context: 'practice' }
  })
}

const handleMaster = (error: any) => {
  console.log('标记为已掌握:', error)
  error.status = 'mastered'
}
</script>

<style scoped>
.student-error-collection {
  min-height: 100vh;
  background-color: #f8f9fa;
}

/* 面包屑导航 */
.breadcrumb {
  padding: 16px 24px;
  font-size: 14px;
  color: #606266;
}

.breadcrumb-item {
  color: #606266;
}

.breadcrumb-item.current {
  color: #303133;
}

.breadcrumb-separator {
  margin: 0 8px;
  color: #c0c4cc;
}

/* 主内容区域 */
.main-content {
  background: #fff;
  margin: 0 24px;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
}

/* 页面标题 */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.page-title {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
  margin: 0;
}

.header-stats {
  display: flex;
  gap: 24px;
}

.stat-item {
  text-align: center;
}

.stat-value {
  display: block;
  font-size: 24px;
  font-weight: 700;
  color: #409eff;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 12px;
  color: #909399;
}

/* 学科分类标签 */
.subject-tabs {
  display: flex;
  gap: 0;
  margin-bottom: 16px;
  border-bottom: 1px solid #e4e7ed;
}

.subject-tab {
  padding: 12px 24px;
  font-size: 14px;
  color: #606266;
  cursor: pointer;
  position: relative;
  transition: all 0.3s ease;
}

.subject-tab.active {
  color: #409eff;
}

.subject-tab.active::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 0;
  right: 0;
  height: 2px;
  background: #409eff;
}

.subject-tab:hover:not(.active) {
  color: #303133;
}

/* 状态分类标签 */
.type-tabs {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 24px;
  flex-wrap: wrap;
}

.type-tab {
  padding: 6px 16px;
  font-size: 14px;
  color: #606266;
  background: #f5f7fa;
  border: 1px solid #e4e7ed;
  border-radius: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.type-tab.active {
  color: #409eff;
  background: #ecf5ff;
  border-color: #b3d8ff;
}

.type-tab:hover:not(.active) {
  color: #303133;
  border-color: #c0c4cc;
}

/* 搜索框 */
.search-box {
  position: relative;
  margin-left: auto;
  width: 240px;
}

.search-input {
  width: 100%;
  padding: 8px 32px 8px 12px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.3s ease;
}

.search-input:focus {
  border-color: #409eff;
}

.search-icon {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  color: #c0c4cc;
  font-size: 16px;
}

/* 错题列表 */
.error-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* 空状态 */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #909399;
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 16px;
}

.empty-text {
  font-size: 16px;
  font-weight: 500;
  color: #606266;
  margin: 0 0 8px 0;
}

.empty-desc {
  font-size: 14px;
  color: #909399;
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .student-error-collection {
    padding: 0;
  }

  .breadcrumb {
    padding: 12px 16px;
  }

  .main-content {
    margin: 0 16px;
    padding: 16px;
  }

  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }

  .header-stats {
    gap: 16px;
  }

  .subject-tabs {
    flex-wrap: wrap;
  }

  .type-tabs {
    flex-wrap: wrap;
    gap: 12px;
  }

  .search-box {
    width: 100%;
    margin-left: 0;
    margin-top: 12px;
  }
}

@media (max-width: 480px) {
  .page-title {
    font-size: 18px;
  }

  .subject-tab {
    padding: 10px 16px;
    font-size: 13px;
  }

  .type-tab {
    padding: 4px 12px;
    font-size: 13px;
  }

  .stat-value {
    font-size: 20px;
  }
}
</style>
